package com.weng.cloud.service8881.mybatis.service.impl;

import com.weng.cloud.commons.base.JsonUtil;
import com.weng.cloud.service8881.mybatis.entity.PlayerEntity;
import com.weng.cloud.service8881.mybatis.mapper.PlayerMapper;
import com.weng.cloud.service8881.mybatis.service.PlayerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: weng
 * @Date: 2022/3/14
 * @Description: com.weng.cloud.service8881.mybatis.service.impl
 */
@Service
public class PlayerServiceImpl implements PlayerService {

    @Resource
    private PlayerMapper playerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int testTransactional(List<PlayerEntity> players) {
        int affectRows = 0;
        for (PlayerEntity player : players) {
            System.err.println("think of me -> "+JsonUtil.toText(player));
            Integer no = player.getNo();
            int i = 10 / no;
            i = this.playerMapper.testTransactional(player);
            affectRows += i;
            System.err.println("id="+player.getId()+" 插入结果 -> "+i);
        }
        return affectRows;
    }

    //测试springaop默认的代理实现的短板——自调用
    //@Transactional(rollbackFor = Exception.class)
    @Override
    public int testTransactionalInner(List<PlayerEntity> players){
        return this.testTransactional(players);
    }
}
